package com.pronhub.luowowo.website.web.advice;

import com.alibaba.fastjson.JSON;
import com.pronhub.luowowo.excption.LoginException;
import com.pronhub.luowowo.util.AjaxResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class CommonExceptionHandler {
    /**
     * 异常的统一处理
     * @param e
     * @param resp
     * @throws IOException
     */
    @ExceptionHandler(LoginException.class)
    public void displayExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(new AjaxResult(false,e.getMessage())));
    }

    @ExceptionHandler(RuntimeException.class)
    public void runTimeExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(new AjaxResult(false,"系统出现未知异常，请联系客服。")));
    }
}
